﻿Public Class UpdateDeleteShopOutlet
    Inherits System.Web.UI.Page

    Public DBManager As DBManager = New DBManager

    'To set masterpage 
    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        'Check if user is null
        If (Session("user") IsNot Nothing) Then
            'Retrieve userAcc details from session
            Dim userLoggedIn As userAcc = Session("user")
            'Check if user is admin, assign masterpage respectively
            If userLoggedIn.isAdmin Then
                Me.MasterPageFile = "~/MasterPage/Admin.Master"
            Else
                Me.MasterPageFile = "~/MasterPage/User.Master"
            End If
        Else
            'For anonymous users
            Me.MasterPageFile = "~/MasterPage/User.Master"
            'Enable this line to prevent anonymous users from entering this page
            'Response.Redirect("~/Login.aspx")
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            Try
                If (Request.QueryString("id") IsNot Nothing) Then
                    Dim fullID = Request.QueryString("id")

                    Dim type = fullID(0)


                    Select Case type
                        Case "b"
                            Dim splitIDString = fullID.Split("b")
                            Dim businessID = splitIDString(1)
                            Dim outletID = DBManager.getOutletIDFromBusinessID(businessID)
                            lblBusinessName.Text = DBManager.getBusinessName(outletID)
                            PanelOutlets.Visible = False
                            ddlReportBusiness.Visible = True
                            rfvBusiness.Enabled = True
                            ddlReportOutlet.Visible = False
                            rfvOutlet.Enabled = False

                        Case "o"
                            Dim splitIDString = fullID.Split("o")
                            Dim outletID = splitIDString(1)
                            lblBusinessName.Text = DBManager.getBusinessName(outletID)
                            populateOutletList(outletID)
                            PanelOutlets.Visible = True
                            ddlReportOutlet.Visible = True
                            rfvOutlet.Enabled = True
                            ddlReportBusiness.Visible = False
                            rfvBusiness.Enabled = False
                    End Select

                Else
                    Response.Redirect("home.aspx")
                End If
            Catch ex As Exception
            End Try
        End If


    End Sub

    'Populate the dropdownlist with all the outlets belonging to the business 
    Protected Sub populateOutletList(ByVal outletID As Integer)
        Dim itemList = DBManager.getListOutlets(outletID)
        For Each outlet As ListItem In itemList
            ddlOutlets.Items.Add(outlet)
        Next
    End Sub

    Protected Sub ButtonDone_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonDone.Click

        If (Request.QueryString("id") IsNot Nothing) Then
            Dim fullID = Request.QueryString("id")

            Dim type = fullID(0)
            Dim redirectID As Integer = 0
            Select Case type
                Case "b"
                    Dim splitIDString = fullID.Split("b")
                    Dim businessID = splitIDString(1)
                    addBusinessReport(businessID)
                    redirectID = DBManager.getOutletIDFromBusinessID(businessID)
                Case "o"
                    addOutletReport()
                    redirectID = ddlOutlets.SelectedValue
            End Select
            Response.Redirect("OutletReview.aspx?id=" + redirectID.ToString)
        Else
            MsgBox("Failed", MsgBoxStyle.OkOnly)
        End If
    End Sub

    Protected Sub addBusinessReport(ByVal businessID As Integer)
        ' deleting and updating priority ranking in the dashboard 
        Dim priority = 2
        Dim resolved = 0
        ' add to the database 
        DBManager.addOutletReport(priority, ddlReportBusiness.SelectedItem.Text, tbxComments.Text, businessID, resolved)
        MsgBox("You have successfully send a feedback.", MsgBoxStyle.OkOnly, "Successful")
        Response.Redirect("~/Home.aspx")

    End Sub

    Protected Sub addOutletReport()
        ' deleting and updating priority ranking in the dashboard 
        Dim priority = 2
        Dim resolved = 0
        ' add to the database 
        DBManager.addOutletReport(priority, ddlReportOutlet.SelectedItem.Text, tbxComments.Text, ddlOutlets.SelectedValue, resolved)
        MsgBox("You have successfully send a feedback.", MsgBoxStyle.OkOnly, "Successful")
        Response.Redirect("~/Home.aspx")
    End Sub

End Class